package tq.com.tqteacher.message.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqteacher.R;
import tq.com.tqteacher.message.model.TQNoticeModel;
import tq.com.tqteacher.message.model.TQUserMsg;

public class TQMessageAdapter extends RecyclerView.Adapter {
    private Context mContext;
    public TQMessageAdapter(Context pContext){
        mContext = pContext;
    }
    private TQNoticeModel mNoticeModel = null;

    private ArrayList<TQUserMsg> mTQUserMsgs = new ArrayList<>();
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        View lView = LayoutInflater.from(mContext).inflate(R.layout.vh_msg_noticebroad_layout, pViewGroup, false);

        return new MsgViewHold(lView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pI == 0){
            ((MsgViewHold)pViewHolder).bindNotic();
        }else{
            int index = pI - 1;
            TQUserMsg lUserMsg = mTQUserMsgs.get(index);

            ((MsgViewHold)pViewHolder).bindMsg(lUserMsg);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0) return 1;
        return 0;
    }

    @Override
    public int getItemCount() {
        return 1 + mTQUserMsgs.size();
    }

    public void setNotice(TQNoticeModel pNoticeModel){
        mNoticeModel = pNoticeModel;
        notifyItemChanged(0);
    }

    public void setTQUserMsgs(ArrayList<TQUserMsg> pUserMsgs){
        this.mTQUserMsgs.clear();
        this.mTQUserMsgs.addAll(pUserMsgs);
        notifyDataSetChanged();
    }

    class MsgViewHold extends RecyclerView.ViewHolder{
        private ImageView mIcon;
        private TextView mTitle;
        private TextView mContent;
        private boolean isNotice;
        private TQUserMsg mTQUserMsg;

        public MsgViewHold(View pView){
            super(pView);

            mIcon = pView.findViewById(R.id.icon);
            mTitle = pView.findViewById(R.id.title);
            mContent = pView.findViewById(R.id.content);

            pView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mClickListener != null){
                        if(isNotice){
                            mClickListener.clickNotice();
                        }else {
                            mClickListener.clickMsg(mTQUserMsg);
                        }
                    }
                }
            });
        }

        public void bindNotic(){
            isNotice = true;
            mIcon.setImageResource(R.drawable.icon_ggl);
            mTitle.setText("公告栏");

            if(mNoticeModel != null){
                mContent.setText(mNoticeModel.getContent());
            }else{
                mContent.setText("");
            }
        }

        public void bindMsg(TQUserMsg pTQUserMsg){
            isNotice = false;
            mTQUserMsg = pTQUserMsg;

            String pic = TQPictureUpload.downloadPath(pTQUserMsg.getUserInfo().getPic());

            RequestOptions lOptions = new RequestOptions().placeholder(R.drawable.tx1).error(R.drawable.tx1)
                    .circleCrop();

            Glide.with(itemView).load(pic).apply(lOptions).into(mIcon);

            mTitle.setText(pTQUserMsg.getUserInfo().getName());
            if(pTQUserMsg.getMsg() != null){
                if(pTQUserMsg.getMsg().getType() == 0){
                    mContent.setText(pTQUserMsg.getMsg().getContent());
                }else if(pTQUserMsg.getMsg().getType() == 1){
                    mContent.setText("图片");
                }else{
                    mContent.setText("视频");
                }

            }else{
                mContent.setText("");
            }
        }
    }

    private IClickListener mClickListener;
    public void setClickListener(IClickListener pListener){
        mClickListener = pListener;
    }
    public interface IClickListener{
        void clickNotice();
        void clickMsg(TQUserMsg pUserMsg);
    }
}
